package com.wxh.jwt.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.wxh.jwt.common.RedisConstant;
import com.wxh.jwt.model.User;
import com.wxh.jwt.service.TokenService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName: TokenServiceImpl
 * @Description: token实现类
 * @Author wxh
 * @Date: 2020/5/28 15:10
 * @Version V1.0.0
 * @Since 1.8
 */
@Service(value = "tokenService")
public class TokenServiceImpl implements TokenService {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String createToken(User user) {
        String token = JWT.create().withAudience(user.getUserId())
                .sign(Algorithm.HMAC256(user.getPassword()));
        redisTemplate.opsForValue().set(RedisConstant.TOKEN_KEY+user.getUserId(), token, RedisConstant.EXPIRE_TIME, TimeUnit.SECONDS);
        return token;
    }


}
